package cn.t.web.user;

import cn.t.converter.user.UserInfoRequestConverter;
import cn.t.core.controller.BaseController;
import cn.t.core.exception.BizException;
import cn.t.dto.user.CreateUserRequest;
import cn.t.dto.user.CustomerFirmDTO;
import cn.t.dto.user.CustomerIndividualDTO;
import cn.t.dto.user.ParticipantInfoDTO;
import cn.t.dto.user.RoleInfoDTO;
import cn.t.dto.user.UpdateUserRequest;
import cn.t.dto.user.UserInfoDTO;
import cn.t.dto.user.UserPassportDTO;
import cn.t.core.enums.base.CommonError;
import cn.t.facade.user.UserInfoFacade;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * 用户信息表服务入口
 *
 * @author l
 */
@Slf4j
@RestController
@RequestMapping("userInfo")

@AllArgsConstructor
public class UserInfoController extends BaseController<UserInfoDTO, UserInfoFacade> {


    @PostMapping("/user/create")
    public UserInfoDTO createUser(@Valid @RequestBody CreateUserRequest request) {

        Map<Integer, UserPassportDTO> passportMap = userInfoRequestConverter.convertPassport(request);
        if (passportMap == null || passportMap.isEmpty()) {
            throw new BizException(CommonError.ILLEGAL_PARAMETER, "通行证参数为空");
        }

        UserInfoDTO userInfo = userInfoRequestConverter.convertUserInfo(request);
        CustomerIndividualDTO customerIndividual = userInfoRequestConverter.convertCustomerInfo(request);
        ParticipantInfoDTO participantInfoDTO = userInfoRequestConverter.convertParticipantInfo(request);
        RoleInfoDTO roleInfoDTO = userInfoRequestConverter.convertRoleInfoDTO(request);

        return facade.create(passportMap, userInfo, participantInfoDTO, roleInfoDTO, customerIndividual, null);
    }


    @PostMapping("/user/update")
    public UserInfoDTO updateUser(@Valid @RequestBody UpdateUserRequest request) {
        Map<Integer, UserPassportDTO> passportMap = userInfoRequestConverter.convertPassport(request);
        UserInfoDTO userInfo = userInfoRequestConverter.convertUserInfo(request);
        CustomerIndividualDTO customerIndividual = userInfoRequestConverter.convertCustomerInfo(request);
        ParticipantInfoDTO participantInfoDTO = userInfoRequestConverter.convertParticipantInfo(request);
        CustomerFirmDTO customerFirmDTO = userInfoRequestConverter.convertCustomerFirm(request);

        facade.update(passportMap, userInfo, participantInfoDTO, customerIndividual, customerFirmDTO);

        return facade.queryOne(request.getUserId());

    }


    @PostMapping("/user/disable")
    public boolean disableUser(Long userId) {
        return facade.disableUser(userId);
    }


    @PostMapping("/user/enable")
    public boolean enableUser(Long userId) {
        return facade.enableUser(userId);
    }


    @PostMapping("/user/changePassword")
    public UserInfoDTO changePassword(Long userId, String password) {
        facade.changePassword(userId, password);
        return facade.queryOne(userId);
    }



    @PostMapping("/user/delete")
    public UserInfoDTO deleteUser(Long userId) {
        facade.deleteUser(userId);
        return facade.queryOne(userId);
    }

    @Autowired
    private UserInfoRequestConverter userInfoRequestConverter;
}

